package cn.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dao.CateMapper;
import cn.domain.Cate;
import cn.domain.CateExample;
import cn.service.CateService;
import cn.util.Pagination;

@Service
@Transactional(rollbackFor=Exception.class)
public class CateServiceImpl implements CateService {

	@Autowired
	private CateMapper catejMapper;

	@Override
	public void insert(Cate  cate) {
		catejMapper.insert(cate);
	}


	@Override
	public int getAllCount(Cate  cate) {
		return catejMapper.getAllCount(cate);
	}


	@Override
	public List<Cate> getAlllist(Pagination pagination) {
		pagination.setStartRow((pagination.getPageIndex() - 1) * pagination.getPageSize());
		List<Cate> list = catejMapper.getAlllist(pagination);
		return list;
	}


	@Override
	public void delete(String deleteId) {
		for(String id:deleteId.split(",")){
			Cate cate = catejMapper.selectByPrimaryKey(Integer.parseInt(id));
			cate.setIsdel(1);
			catejMapper.updateByPrimaryKey(cate);
		}
	}

	@Override
	public boolean getOne(Cate cate) {
		CateExample example = new CateExample();
		example.createCriteria().andCnameEqualTo(cate.getCname()).andIsdelEqualTo(0);
		List<Cate> cates = catejMapper.selectByExample(example);
		if(!cates.isEmpty() && cates.size() > 0){
			return true;
		}
		return false;
	}


	@Override
	public Cate selectOne(String deleteId) {
		return catejMapper.selectByPrimaryKey(Integer.parseInt(deleteId));
	}


	@Override
	public void update(Cate cate) {
		catejMapper.updateByPrimaryKey(cate);
	}


	@Override
	public List<Cate> getAll() {
		return catejMapper.getAll();
	}


	@Override
	public List<Cate> getCatesByAid(Integer aid) {
		CateExample example = new CateExample();
		example.createCriteria().andAidEqualTo(aid);
		return catejMapper.selectByExample(example);
	}

}
